package com.wiz.web.controllor;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.service.GradeConfigService;
import com.wiz.to.GradeConfigTO;
import com.wiz.vo.ResponseJsonBody;

/**
 * 总部会员等级配置信息
 * @author xxy
 *
 */
@Controller
@RequestMapping("/gradeConfig")
public class GradeConfigControllor {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Resource
	private GradeConfigService gradeConfigService;
	
	
	@RequestMapping(value="/toadd")
	public String toAdd(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model){
		return "gradeConfig/add";
		
	}
	@RequestMapping(value="/toUpdate")
	public String toUpdate(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "id", required = true) int id,
			HttpSession session, Model model){
		GradeConfigTO GradeConfigTO = gradeConfigService.getGradeConfig(id);
		model.addAttribute("bean", GradeConfigTO);
		return "gradeConfig/update";
		
	}
	@RequestMapping(value="/add")
	@ResponseBody
	public String add(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "point", required = true) int point,
			@RequestParam(value = "saleAmount", required = true) double saleAmount,
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		GradeConfigTO gradeConfigTO = new GradeConfigTO(0, name, point, saleAmount, new Date());
		try{
			gradeConfigService.add(gradeConfigTO);
			json = new ResponseJsonBody("200", "保存成功", "grade_config_list",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "grade_config_list",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
		
	}
	@RequestMapping(value="/update")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "id", required = true) int id,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "point", required = true) int point,
			@RequestParam(value = "saleAmount", required = true) double saleAmount,
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		GradeConfigTO gradeConfigTO = new GradeConfigTO(id, name, point, saleAmount,null);
		try{
			gradeConfigService.update(gradeConfigTO);
			json = new ResponseJsonBody("200", "保存成功", "grade_config_list",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "grade_config_list",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
		
	}
	@RequestMapping(value="/list")
	public String list(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model){
		List<GradeConfigTO>list = gradeConfigService.list();
		model.addAttribute("list", list);
		return "gradeConfig/list";
		
	}
	@RequestMapping(value="/del")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "id", required = true) int id,
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		try {
			gradeConfigService.remove(id);
			json = new ResponseJsonBody("200","删除成功！" , "grade_config_list",
					"", "", "", "");
		} catch (Exception e) {
			json = new ResponseJsonBody("300", e.getMessage(), "grade_config_list",
					"", "", "", "");
			e.printStackTrace();
		}
		return gson.toJson(json) ;
		
	}

}
